package com.ruoyi.project.zerocarbon.service.impl;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.QiniuUtils;
import com.ruoyi.project.zerocarbon.domain.*;
import com.ruoyi.project.zerocarbon.mapper.DeclarationGansuMapper;
import com.ruoyi.project.zerocarbon.mapper.DeclareJudgeMapper;
import com.ruoyi.project.zerocarbon.mapper.DeclareRankingMapper;
import com.ruoyi.project.zerocarbon.service.IDeclarationGansuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.file.QiniuUtils.getQiniuPath;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author www.joolun.com
 * @date 2022-05-27
 */
@Slf4j
@Service
public class DeclarationGansuServiceImpl implements IDeclarationGansuService
{
    @Autowired
    private DeclarationGansuMapper declarationGansuMapper;
    @Resource
    private QiniuUtils qiniuUtils;
    @Autowired
    private DeclareJudgeMapper declareJudgeMapper;
    @Autowired
    private DeclareRankingMapper declareRankingMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public DeclarationGansu selectDeclarationGansuById(Long id)
    {
        return declarationGansuMapper.selectDeclarationGansuById(id);
    }

    @Override
    public DeclarationGansu selectDeclarationGansuByUserName(String userName) {
        DeclarationGansu declarationCheck = new DeclarationGansu();
        declarationCheck.setUserName(userName);
        DeclarationGansu declarationGansu = declarationGansuMapper.selectDeclarationGansuListByUserName(userName);
        return declarationGansu;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param declarationGansu 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DeclarationGansu> selectDeclarationGansuList(DeclarationGansu declarationGansu)
    {
        return declarationGansuMapper.selectDeclarationGansuList(declarationGansu);
    }

    @Override
    public List<DeclarationGansu> selectDeclarationGansuListExport(DeclarationGansu declarationGansu) {
        return declarationGansuMapper.selectDeclarationGansuListExport(declarationGansu);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param declarationGansu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDeclarationGansu(DeclarationGansu declarationGansu)
    {
        if (declarationGansu.getSaveType() != null && declarationGansu.getSaveType() != 0){
            String maxSerial = declarationGansuMapper.selectMaxSerial();
            if (StringUtils.isEmpty(maxSerial) || Integer.valueOf(maxSerial) == 0){
                declarationGansu.setRegionSerial("000001");
            }else {
                declarationGansu.setRegionSerial(NumberUtils.buildSerial(Integer.valueOf(maxSerial)));
            }
        }
        DeclarationGansu declarationCheck = new DeclarationGansu();
        declarationCheck.setUserName(declarationGansu.getUserName());
        List<DeclarationGansu> declarationHistoryList = declarationGansuMapper.selectDeclarationGansuList(declarationCheck);
        int saveSuccess = 0;
        if (CollectionUtils.isNotEmpty(declarationHistoryList)){
            saveSuccess = 1;
            DeclarationGansu declarationHistory = declarationHistoryList.get(0);
            if (declarationHistory.getSaveType() != null && declarationHistory.getSaveType() ==1 && declarationHistory.getAuditStatus() != 14){
                throw new CustomException("申报已经提交，不可修改");
            }
            declarationGansu.setId(declarationHistory.getId());
            declarationGansu.setUpdateTime(DateUtils.getNowDate());
            declarationGansu.setAuditStatus(11);
            declarationGansuMapper.updateDeclarationGansu(declarationGansu);
        }else {
            declarationGansu.setCreateTime(DateUtils.getNowDate());
            declarationGansu.setAuditStatus(11);
            saveSuccess = declarationGansuMapper.insertDeclarationGansu(declarationGansu);
        }
        if (saveSuccess > 0){
            Long declarationId = declarationGansu.getId();
            if (declarationGansu.getSaveType() != null && declarationGansu.getSaveType() ==1){
                if(StringUtils.isEmpty(declarationGansu.getSupplementaryMaterialsPath()) && StringUtils.isEmpty(declarationGansu.getVideoPath())){
                    return saveSuccess;
                }
                //打包zip
                String zipPath = "declaration/zip/"+declarationGansu.getHeadName().trim() + "-" + declarationGansu.getRegionSerial() + ".zip";
                List<String> collect = new ArrayList<>();
                if(StringUtils.isNotEmpty(declarationGansu.getVideoPath())){
                    collect.add(declarationGansu.getVideoPath());
                }
                if(StringUtils.isNotEmpty(declarationGansu.getSupplementaryMaterialsPath())){
                    collect.add(declarationGansu.getSupplementaryMaterialsPath());
                }
                String zipid = qiniuUtils.zipFiles(collect,zipPath);
                DeclarationGansu declarationGansuFile = new DeclarationGansu();
                declarationGansuFile.setId(declarationId);
                declarationGansuFile.setQiniuzipid(zipid);
                declarationGansuFile.setQiniuzippath(getQiniuPath(zipPath));
                log.info("打包zip QiniuPath: {},zipid:{}",declarationGansuFile.getQiniuzippath(),zipid);
                declarationGansuMapper.updateDeclarationGansu(declarationGansuFile);
            }
        }
        return saveSuccess;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param declarationGansu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDeclarationGansu(DeclarationGansu declarationGansu)
    {
        declarationGansu.setUpdateTime(DateUtils.getNowDate());
        return declarationGansuMapper.updateDeclarationGansu(declarationGansu);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDeclarationGansuByIds(Long[] ids)
    {
        return declarationGansuMapper.deleteDeclarationGansuByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDeclarationGansuById(Long id)
    {
        return declarationGansuMapper.deleteDeclarationGansuById(id);
    }

    @Override
    public List<DeclarationGansu> getJudgeDecorationListByVoList(List<DeclarationGansu> groupDecorationList, Integer judgeStatus, String expertNumber) {
        List<DeclarationGansu> list = new ArrayList<>();
        for (DeclarationGansu declaration:groupDecorationList){
            DeclareJudge declareJudge = new DeclareJudge();
            declareJudge.setDeclarationId(declaration.getId());
            declareJudge.setExpertNumber(expertNumber);
            if (judgeStatus != null){
                declareJudge.setStatus(judgeStatus);
            }
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeList(declareJudge);
            if (CollectionUtils.isNotEmpty(declareJudges)){
                declaration.setScore(declareJudges.get(0).getScore());
                declaration.setJudgeStatus(declareJudges.get(0).getStatus());
            }else {
                //                if (judgeStatus != null && judgeStatus == 0){
                //                    continue;
                //                }
                declaration.setJudgeStatus(0);
            }
            // 推荐星级
            DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declaration.getId());
            Integer score = 0;
            if (declareRanking != null){
                score = declareRanking.getScore();
            }
            int ranking = declarationGansuMapper.getRankingNum(score) + 1;
            declaration.setRanking(ranking);
            list.add(declaration);
        }
        return list;
    }

    @Override
    public List<DeclarationGansu> getRankingDecorationList(DeclarationGansu declarationGansu) {
        List<DeclarationGansu> rankingDecorationList = declarationGansuMapper.getRankingDecorationList(declarationGansu);
        for (DeclarationGansu declaration:rankingDecorationList){
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeByDecorationId(declaration.getId());
            declaration.setDeclareJudges(declareJudges);
            if (!CollectionUtils.isEmpty(declareJudges)){
                //                if (declareJudges.size() < 2){
                //                    declaration.setScore(0);
                //                }else if (declareJudges.size() == 3){
                //                    Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
                //                    declaration.setScore(Math.round(sum/3));
                //                }else if (declareJudges.size() == 2){
                //                    Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
                //                    declaration.setScore(Math.round(sum/3));
                //                }
                Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
                declaration.setScore(Math.round(sum/4));
            }else {
                declaration.setScore(0);
            }
            DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declaration.getId());
            Integer score = 0;
            if (declareRanking != null){
                score = declareRanking.getScore();
            }
            int ranking = declarationGansuMapper.getRankingNum(score) + 1;
            declaration.setRanking(ranking);
        }
        return rankingDecorationList;
    }
}
